from PyQt4 import QtCore

screens = None
current = None

def add(name, widget):
	global screens

	print "screen manager: adding ", name
	if screens == None:
		screens = {}
	screens[name] = widget
	widget.setFocusPolicy(QtCore.Qt.StrongFocus)


def show(name):
	global screens
	global current

	print "screen manager: showing ", name
	screens[current].hide()
	current = name
	screens[current].show()
	screens[current].setFocus(True)

def show_current():
	global screens
	global current

	screens[current].show()
	screens[current].setFocus(True)

def popup(name):
	screens[name].show()
	
def popdown(name):
	screens[name].hide()

def message_box(message, title="", button="", func=None):
	mbox = screens["message box"]
	if mbox == None:
		raise Exception("can't find message box screen")
#
#	title and button have reasonable defaults in the 
#	MsgBox class, so only set them if supplied
#
	if title != "":
		mbox.setTitle(title)
	if button != "":
		mbox.setButton(button)
#
#	we do however need the message
#
	mbox.setMessage(message)
#
#       default the func to closing the popup
#
	if func == None:
		func = close_mbox
	mbox.setHandler(func)
#
#       and show the popup
#
	mbox.show()

def close_mbox():
	popdown("message box")


def printer(message):
	mscreen = screens["main screen"]
	if mscreen == None:
		raise Exception("can't find main screen")
	mscreen.printer.set_text(
		"{}".format(message)
	)


